package com.nidu.demo.web.system;

import com.alibaba.cola.dto.MultiResponse;
import com.alibaba.cola.dto.PageResponse;
import com.alibaba.cola.dto.Response;
import com.alibaba.cola.dto.SingleResponse;
import com.alibaba.fastjson2.JSONArray;
import com.nidu.demo.base.BatchDeleteCmd;
import com.nidu.demo.mail.api.SysMailAccountService;
import com.nidu.demo.mail.dto.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashSet;
import java.util.List;

/**
 * 邮件账号管理
 *
 * @author auto
 */
@Tag(name = "管理后台 - 邮件账号")
@RestController
@RequestMapping("/system/mail-account")
@Validated
@RequiredArgsConstructor
public class SysMailAccountController {

    private final SysMailAccountService mailAccountService;

    @Operation(summary = "创建邮件账号", description = "创建新的邮件账号")
    @PostMapping
    @PreAuthorize("@ss.hasPermission('system:mail-account:create')")
    public SingleResponse<MailAccountCO> addMailAccount(@RequestBody MailAccountAddCmd cmd) {
        return mailAccountService.add(cmd);
    }

    @Operation(summary = "更新邮件账号", description = "更新邮件账号的信息")
    @PutMapping("/{id}")
    @PreAuthorize("@ss.hasPermission('system:mail-account:update')")
    public SingleResponse<MailAccountCO> updateMailAccount(@PathVariable Long id, @RequestBody MailAccountUpdateCmd cmd) {
        cmd.setId(id);
        return mailAccountService.update(cmd);
    }

    @Operation(summary = "删除邮件账号", description = "根据 ID 删除邮件账号")
    @DeleteMapping("/{id}")
    @PreAuthorize("@ss.hasPermission('system:mail-account:delete')")
    public Response deleteMailAccount(@PathVariable Long id) {
        return mailAccountService.delete(id);
    }

    @Operation(summary = "批量删除邮件账号", description = "根据 ID 列表删除多个邮件账号")
    @PostMapping("/batch")
    @PreAuthorize("@ss.hasPermission('system:mail-account:delete')")
    public Response deleteMailAccounts(@RequestBody BatchDeleteCmd cmd) {
        List<Long> ids = JSONArray.parseArray(cmd.getDelete(), Long.class);
        return mailAccountService.delete(new HashSet<>(ids));
    }

    @Operation(summary = "获取邮件账号详情", description = "根据 ID 获取邮件账号详细信息")
    @GetMapping("/{id}")
    @PreAuthorize("@ss.hasPermission('system:mail-account:query')")
    public SingleResponse<MailAccountCO> getMailAccountById(@PathVariable Long id) {
        return mailAccountService.getById(id);
    }

    @Operation(summary = "查询邮件账号列表", description = "获取所有邮件账号信息")
    @GetMapping("/list")
    @PreAuthorize("@ss.hasPermission('system:mail-account:query')")
    public MultiResponse<MailAccountCO> listMailAccounts(MailAccountListQry qry) {
        return mailAccountService.list(qry);
    }

    @Operation(summary = "分页查询邮件账号", description = "分页获取邮件账号信息")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPermission('system:mail-account:query')")
    public PageResponse<MailAccountCO> pageMailAccounts(MailAccountPageQry qry) {
        return mailAccountService.page(qry);
    }

}
